package com.fishsticks.view;

import java.util.ArrayList;

import com.fishsticks.R;
import com.fishsticks.model.Category;
import com.fishsticks.model.CategoryAdapter;
import com.fishsticks.model.DatabaseManager;
import com.fishsticks.model.NewCategoryDialog;
import com.fishsticks.model.ToDoAdapter;
import com.fishsticks.model.ToDoItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class CategoryListActivity extends Activity {

	private ListView categoryList;
	private DatabaseManager db;
	private String currentUser;
	private ArrayList<Category> categories;
	private CategoryAdapter categoriesAdapter;
	private Bundle extras;
	private EditText newCategoryField;
	private AlertDialog.Builder categoryDialog;
	
	/**
     * Called when the activity is first created.
     * Initializes home screen to-do list page 
     * and action bar buttons on top
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        this.setContentView(R.layout.categories_page);

        categoryDialog = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        
        categoryList = (ListView) findViewById(R.id.categories_list);
        
        db = new DatabaseManager(this);
        currentUser = db.getLoggedInUser();
        categories = db.getCategories(currentUser);
        
        categoriesAdapter = new CategoryAdapter(this, R.layout.category_list_item_row, categories);
        categoryList.setAdapter(categoriesAdapter);
        categoriesAdapter.setNotifyOnChange(true);
        
        extras = getIntent().getExtras();
        
        if(extras != null) 
        {
        	if(extras.getBoolean("refreshList")) 
        		categoriesAdapter.notifyDataSetChanged();
        }
        
        db.close();
    }
    
    
    
    /**
     * Creates the menu options in the action bar at the top.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.categories_page_menu, menu);
        return true;
    }
    
    
    
    /**
     * Listeners for the top bar menu items
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	Intent i = null;
        switch (item.getItemId()) 
        {
        	//When the user clicks the top-left home icon
            case android.R.id.home:
                i = new Intent(this, ToDoListActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(i);
                return true;
                
            //shows the new category dialog when plus button clicked
            case R.id.add_new_category:
            	Context mContext = getBaseContext();
            	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            	View categoryDialogView = inflater.inflate(R.layout.categories_dialog_box, (ViewGroup)findViewById(R.id.categories_dialog_box_root));
            	
            	//Gets the check box in the dialog
            	final EditText newCategoryName = (EditText) categoryDialogView.findViewById(R.id.new_category_field);
            	
            	categoryDialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        			@Override
        			public void onClick(DialogInterface dialog, int which) {
        				
        				if(newCategoryName.getText().toString() != "") {
	        				//clears the current to-do list
	        				categoriesAdapter.clear();
	        				categoriesAdapter.notifyDataSetChanged();
	        				
	        				db.addCategory(newCategoryName.getText().toString().trim());
	        				categories = db.getCategories(currentUser);
	        				
	        				//Create new adapter to bind data elements to list view
	        				CategoryAdapter newAdapter = new CategoryAdapter(CategoryListActivity.this, R.layout.category_list_item_row, categories);
	        				categoryList.setAdapter(newAdapter);
	        				newAdapter.notifyDataSetChanged();
        				}
        				
        				else {
        					newCategoryName.setHint("The name can't be empty!");
        				}
        			}
            	});
            	
            	categoryDialog.setView(categoryDialogView);
            	categoryDialog.show();

            	return true;
                
            default:
                return super.onOptionsItemSelected(item);
        }
    }   
}
